package JZ_Offer.algorithm.other.字符串;

/**
 * @Author piper
 * @Date 2019/11/4 7:16 下午
 */
public class JZ5_替换空格 {
    private static String replaceSpace1(StringBuffer str) {
        return str.toString().replace(" ", "%20");
    }

    private static String replaceSpace2(StringBuffer str) {
        int index = 0;
        while (index < str.length()) {
            index = str.indexOf(" ", index);
            if (index != -1) {
                str.replace(index, ++index, "%20");
            } else {
                break;
            }
        }
        return str.toString();
    }

    public static void main(String[] args) {
        System.out.println(replaceSpace2(new StringBuffer("We Are Happy")));
    }
}
